@echo off

set common=0
set main=0
set monitor=0
set logger=0
set core=0
set commonpath=%~dp0lib\uoamp-agent-common.new.jar
set mainpath=%~dp0uoamp-agent-main.new.jar
set monitorpath=%~dp0lib\uoamp-agent-monitor.new.jar
set loggerpath=%~dp0lib\uoamp-agent-logger.new.jar
set corepath=%~dp0lib\uoamp-agent-core.new.jar
call:main
goto:eof

:main
	::back-up running jar.rename download jar file to running jar
	call:copyJar
	::restart jar, rename log file to back-up log file
	call:restartJar
	call:timePush
	call:isValidJar
    call:install
goto:eof


:copyJar
	::delete running program
	taskkill /im uoampagent.exe /f
	::back-up old jar file
	echo back jar >> ./redeploy.log
	if exist %corepath% (
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-core.new.jar %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.jar
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.bak.jar
		set core=1
		echo core=1 >> ./redeploy.log
	)
	
	if exist %monitorpath% (
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-monitor.new.jar %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.jar
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.bak.jar
		set monitor=1
		echo monitor=1 >> ./redeploy.log
	)

	if exist %commonpath% (
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-common.new.jar %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.jar
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.bak.jar
		set common=1
		echo common=1 >> ./redeploy.log
	)

	if exist %loggerpath% (
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-logger.new.jar %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.jar
		echo f|xcopy /Y /F %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.bak.jar
		set logger=1
		echo logger=1 >> ./redeploy.log
	)

	if exist %mainpath% (
		echo f|xcopy /Y /F uoamp-agent-main.new.jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
		echo f|xcopy /Y /F uoamp-agent-main-0.0.1-SNAPSHOT.jar uoamp-agent-main-0.0.1-SNAPSHOT.bak.jar
		set main=1
		echo main=1 >> ./redeploy.log
	)				
	echo back jar finished!>> ./redeploy.log
goto:eof

:restartJar
	::start program
	start /b uoampagent.exe -jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
	echo restart jar success >> ./redeploy.log
goto:eof

:isValidJar
	tasklist | findstr uoampagent.exe > nul
	if %errorlevel% == 1 (	
		call :rebackJar
		start /b uoampagent.exe -jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
		echo jar redeploy fail, reback jar >> ./redeploy.log
	) else (
		echo jar redeploy success >> ./redeploy.log
		echo f|del /q uoamp-agent-main.new.jar
		echo f|del /q %~dp0lib\uoamp-agent-core.new.jar
		echo f|del /q %~dp0lib\uoamp-agent-monitor.new.jar
		echo f|del /q %~dp0lib\uoamp-agent-common.new.jar
		echo f|del /q %~dp0lib\uoamp-agent-logger.new.jar
	)
goto:eof

:timePush
	::use ping to delay time, like sleep
	ping -n 3 127.0.0.1 >nul 
goto:eof

:rebackJar
    if %main% == 1 (	
	    echo f|xcopy /Y /F uoamp-agent-main-0.0.1-SNAPSHOT.jar uoamp-agent-main.new.jar
	    echo f|xcopy /Y /F uoamp-agent-main-0.0.1-SNAPSHOT.bak.jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
	    echo f|del /q uoamp-agent-main-0.0.1-SNAPSHOT.bak.jar
    )
    
    if %core% == 1 (	
        echo f|xcopy /Y /F %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-core.new.jar
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.bak.jar %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.jar
	    echo f|del /q %~dp0lib\uoamp-agent-core-0.0.1-SNAPSHOT.bak.jar
    )

    if %common% == 1 (	
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-common.new.jar
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.bak.jar %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.jar
	    echo f|del /q %~dp0lib\uoamp-agent-common-0.0.1-SNAPSHOT.bak.jar    
    )

    if %logger% == 1 (	
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-logger.new.jar
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.bak.jar %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.jar
	    echo f|del /q %~dp0lib\uoamp-agent-logger-0.0.1-SNAPSHOT.bak.jar    
    )

    if %monitor% == 1 (	
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.jar %~dp0lib\uoamp-agent-monitor.new.jar
	    echo f|xcopy /Y /F %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.bak.jar %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.jar
	    echo f|del /q %~dp0lib\uoamp-agent-monitor-0.0.1-SNAPSHOT.bak.jar   
    )                
goto:eof
  
:install
    sc query "UoampAgentService" >nul
    if errorlevel 1060 goto self-starting   
goto:eof

:self-starting
    set serviceName=UoampAgentService
    set programeName=uoampagent.bat
    set dir=%windir%\System32
    for /f "tokens=*" %%b in ('systeminfo^|find "x64"') do set BIT_FLAG=true
    if "%BIT_FLAG%"=="true"  (
        set dir=%windir%\SysWow64
    )

    copy %~dp0srvany\instsrv.exe %dir%
    copy %~dp0srvany\srvany.exe %dir%

    %dir%\instsrv.exe %serviceName% %dir%\srvany.exe
    echo instsrv Parameters! > ./install.log

    set curExe=%~dp0%programeName%
    echo add Parameters! > ./install.log

    set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\%serviceName%\Parameters\

    reg add %regpath% /v AppDirectory /t REG_SZ /d "%~dp0\" /f

    reg add %regpath% /v Application /t REG_SZ /d "%curExe%" /f 

    reg add %regpath% /v AppParameters /t REG_SZ /d "start" /f
    echo install service succss! >> ./install.log
goto:eof	